const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const threadLoader = require('thread-loader');

threadLoader.warmup({
	// pool options, like passed to loader options
	// must match loader options to boot the correct pool
	cacheDirectory: true,
}, [
	'vue-loader',
	'babel-loader',
]);

module.exports = {
	entry: {
		main: './src/main',
	},
	output: {
		path: path.join(__dirname, '../../public/web-admin/dist'),
	},
	module: {
		rules: [
			{
				test: /\.vue$/,
				// loader: 'vue-loader',
				use: [
					{
						loader: 'thread-loader',
					},
					'vue-loader',
				],
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, 'node_modules', 'iview/src'),
					path.resolve(__dirname, 'node_modules', 'vue-scroll-bar/src'),
				],
			},
			{
				test: /iview.*js$/,
				use: [
					{
						loader: 'thread-loader',
					},
					{
						loader: 'babel-loader',
						options: {
							cacheDirectory: true,
						},
					},
				],
				include: path.resolve(__dirname, 'src'),
			},
			{
				test: /\.js$/,
				use: [
					{
						loader: 'thread-loader',
					},
					{
						loader: 'babel-loader',
						options: {
							cacheDirectory: true,
						},
					},
				],
				include: path.resolve(__dirname, 'src'),
			},
			{
				test: /\.css$/,
				use: [
					process.env.NODE_ENV !== 'production'
						? 'vue-style-loader'
						: MiniCssExtractPlugin.loader,
					{
						loader: 'css-loader',
						options: {
							sourceMap: true,
						},
					},
					'postcss-loader',
				],
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, 'node_modules', 'iview/dist/styles'),
					path.resolve(__dirname, 'node_modules', 'vue-scroll-bar/src'),
				],
			},
			{
				test: /\.less$/,
				use: [
					process.env.NODE_ENV !== 'production'
						? 'vue-style-loader'
						: MiniCssExtractPlugin.loader,
					{
						loader: 'css-loader',
						options: {
							sourceMap: true,
						},
					},
					'postcss-loader',
					'less-loader',
				],
				include: [
					path.resolve(__dirname, 'src'),
				],
			},
			{
				test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
				loader: 'url-loader?limit=1024',
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, 'node_modules', 'iview/dist/styles'),
				],
			},
			{
				test: /\.(html|tpl)$/,
				loader: 'html-loader',
			},
		],
	},
	optimization: {
		splitChunks: {
			cacheGroups: {
				styles: {
					name: 'styles',
					test: /\.css$/,
					chunks: 'all',
					enforce: true,
				},
			},
		},
	},
	resolve: {
		extensions: ['.js', '.vue'],
		alias: {
			vue: 'vue/dist/vue.esm.js',
			src: path.resolve(__dirname, './src'),
		},
	},
};
